package com.dd.common.enums;


/**
 * 错误码枚举
 */
public enum ErrorEnum {

    // ================== 基础错误 (1000-1999) ==================
    SUCCESS(1000, 200, "操作成功"),
    SYSTEM_ERROR(1001, 500, "系统内部错误"),
    SERVICE_UNAVAILABLE(1002, 503, "服务暂时不可用"),
    API_DEPRECATED(1003, 410, "API已弃用"),
    NOT_FOUND(1004, 404, "资源未找到"),

    // ================== 参数错误 (2000-2999) ==================
    BAD_REQUEST(2000, 400, "请求参数错误"),
    PARAMS_VALID_ERROR(2001, 400, "参数校验失败"),
    PARAMS_TYPE_ERROR(2002, 400, "参数类型错误"),
    PARAMS_MISSING(2003, 400, "必要参数缺失"),
    PARAMS_FORMAT_ERROR(2004, 400, "参数格式错误"),
    REQUEST_METHOD_ERROR(2005, 405, "请求方法不允许"),
    UNSUPPORTED_CONTENT_TYPE(2006, 415,"不支持的Content-Type"),

    // ================== 认证授权 (3000-3999) ==================
    UNAUTHORIZED(3000, 401, "未授权访问"),
    FORBIDDEN(3001, 403, "访问权限不足"),
    TOKEN_INVALID(3002, 401, "凭证无效"),
    TOKEN_EXPIRED(3003, 401, "凭证已过期"),
    TOKEN_EMPTY(3004, 401, "凭证缺失"),
    CAPTCHA_ERROR(3005, 401, "验证码错误"),
    CAPTCHA_EXPIRED(3006, 401, "验证码已过期"),
    LOGIN_FAILED(3007, 401, "登录失败"),
    ACCOUNT_LOCKED(3008, 403, "账号已被锁定"),
    PASSWORD_ERROR(3009, 401, "密码错误"),
    PASSWORD_EXPIRED(30010, 403, "密码已过期"),
    OLD_PASSWORD_ERROR(3011, 400, "原密码错误"),

    // ================== 用户相关 (4000-4999) ==================
    USER_EXIST(4000, 409, "用户已存在"),
    USER_NOT_FOUND(4001, 404, "用户不存在"),
    USER_DISABLED(4002, 403, "用户已被禁用"),
    USER_AVATAR_ERROR(4003, 400, "头像上传失败"),
    USER_PROFILE_ERROR(4004, 400, "资料更新失败"),

    // ================== 业务操作 (5000-5999) ==================
    OPERATION_FAILED(5000, 400, "操作执行失败"),
    DATA_NOT_FOUND(5001, 404, "数据不存在"),
    DATA_EXISTS(5002, 409, "数据已存在"),
    DATA_INVALID(5003, 400, "数据状态异常"),
    DATA_DUPLICATION(5004, 409, "数据重复提交"),

    // ================== 文件操作 (6000-6999) ==================
    FILE_UPLOAD_ERROR(6000, 400, "文件上传失败"),
    FILE_DOWNLOAD_ERROR(6001, 400, "文件下载失败"),
    FILE_TYPE_ERROR(6002, 400, "文件类型不支持"),
    FILE_SIZE_EXCEEDED(6003, 400, "文件大小超出限制"),
    FILE_NOT_FOUND(6004, 404, "文件不存在"),
    FILE_ALREADY_EXISTS(6005, 409, "文件已存在"),
    FILE_DELETE_ERROR(6006, 400, "文件删除失败"),

    // ================== 第三方服务 (7000-7999) ==================
    PAYMENT_ERROR(7000, 400, "支付服务异常"),
    SMS_SEND_ERROR(7001, 400, "短信发送失败"),
    EMAIL_SEND_ERROR(7002, 400, "邮件发送失败"),
    THIRD_PARTY_TIMEOUT(7003, 504, "第三方服务超时"),

    // ================== 数据库相关 (8000-8999) ==================
    DB_CONNECTION_ERROR(8000, 500, "数据库连接失败"),
    DB_QUERY_ERROR(8001, 500, "数据库查询异常"),
    DB_TRANSACTION_ERROR(8002, 500, "数据库事务异常"),
    DB_DUPLICATE_ENTRY(8003, 409, "数据库唯一约束冲突"),

    // ================== 系统配置 (9000-9999) ==================
    CONFIG_ERROR(9000, 500, "系统配置错误"),
    FEATURE_DISABLED(9001, 501, "功能暂未开放");

    /**
     * 响应码
     */
    private final int code;
    /**
     * HTTP 状态码
     */
    private final int httpStatus;
    /**
     * 说明信息
     */
    private final String msg;

    ErrorEnum(int code, int httpStatus, String msg) {
        this.code = code;
        this.httpStatus = httpStatus;
        this.msg = msg;
    }

    public int getCode() {
        return this.code;
    }
    public int getHttpStatus() {
        return this.httpStatus;
    }
    public String getMsg() {
        return this.msg;
    }

    // 可选：添加根据 code 查找枚举的方法
    public static ErrorEnum fromCode(int code) {
        for (ErrorEnum error : ErrorEnum.values()) {
            if (error.getCode() == code) {
                return error;
            }
        }
        return SYSTEM_ERROR; // 默认回退
    }
}